module.factory('langService', [
		'$rootScope',
		'$http',
		function($rootScope, $http) {

			$rootScope.lang = 'vi';

			// Reset languages
			$rootScope.resetLang = function() {
				$rootScope.langCommon = null;
				//$rootScope.langSearch = null;
				$rootScope.langProduct = null;
				$rootScope.errMsg = null;
				$rootScope.langError = null;
			};

			// Get languages
			$rootScope.getLangCommon = function() {
				if ($rootScope.langCommon == null) {
					$http.post(
							DOMAIN + '/json/lang/common_' + $rootScope.lang
									+ '.json').success(function(data) {
						$rootScope.langCommon = data;
					});
				}
				if ($rootScope.errMsg == null) {
					$http.post(
							DOMAIN + '/json/data/err_' + $rootScope.lang
									+ '.json').success(function(data) {
						$rootScope.errMsg = data;
					});
				}
			};

			$rootScope.getLangError = function() {
				if ($rootScope.langError == null) {
					$http.post(
							DOMAIN + '/json/lang/error_' + $rootScope.lang
									+ '.json').success(function(data) {
						$rootScope.langError = data;
					});
				}
			};
			
			$rootScope.getLangProduct = function() {
				if ($rootScope.langProduct == null) {
					$http.post(
							DOMAIN + '/json/lang/prodcut_' + $rootScope.lang
									+ '.json').success(function(data) {
						$rootScope.langProduct = data;
					});
				}
			};

			// Get specified language for current page
			$rootScope.chageLanguage = function() {
				$rootScope.resetLang();
				$rootScope.getLangCommon();

				var location = window.location.pathname;
				if (location.indexOf('product') > -1) {
					$rootScope.getLangProduct;
				} else if (location.indexOf('error') > -1) {
					$rootScope.getLangError();
				}
			};

			// Initial 
			// $http.post(DOMAIN + '/data/user/lang/get').success(function(data)
			// {
			// data = data.substring(1, data.length - 1);
			// $rootScope.lang = data;
			// $rootScope.chageLanguage();
			//			});

			var instance = {
				setLanguage : function(lang) {
					$rootScope.lang = lang;
					// $http.post(DOMAIN + '/data/user/lang/update/' + lang)
					// .success(function(data) {
					//							});
					$rootScope.chageLanguage();
				},

			};
			return instance;
		} ]);